<?php
    include 'template/header.html';
require_once 'lib/function.php';

    $action = isset($_GET['action'])?$_GET['action']:'list';
    switch ($action) {
        case 'list':
            $sql = "SELECT
caretype.ct_type,
customercare.ccare_theme,
customercare.ccare_time,
customercare.ccare_nexttime,
customercare.ccare_comment,
employee.e_name,
customer.c_name,
customercare.ccare_id
FROM
caretype
inner JOIN customercare ON caretype.ct_id = customercare.ct_id
inner JOIN employee ON customercare.e_id = employee.e_id
inner JOIN customer ON customercare.c_id = customer.c_id
order by customercare.ccare_id";
            $rowNum = getRowNum($sql);
            $page = isset($_GET['page'])?$_GET['page']:1;
            $listNum = 6;
            $maxPage = ceil($rowNum/$listNum);
            $res = page($page,$sql, $listNum);
            include 'template/customer_care_list.html';

            break;
        case 'add':
            $sql = "select ct_id,ct_type from caretype";
            $sql1 = "select c_id,c_name from customer";
            $ctypeArr = getAllData($sql);
            $cArr = getAllData($sql1);
            include 'template/customer_care_add.html';
            break;
        case 'add_check':
            $care_theme = $_POST['care_theme'];
            $care_obj = $_POST['care_obj']+0;
            $sql = "select e_id from customer where c_id='$care_obj'";
            $res = getAllData($sql);
            $e_id = $res[0][0];
            $care_nexttime = $_POST['care_nexttime'];
            $care_type = $_POST['care_type']+0;
            $care_comment = $_POST['care_comment'];
            $date = date("Y-m-d H:i:s");
            $sql = "insert into customercare values('','$care_obj','$care_theme','$care_type','$date','$care_nexttime','$e_id','$care_comment')";

            $res = dclData($sql);
            if($res){
                echo "<script>alert('添加成功!');location.href='ccare.php'</script>";
            }
            break;
        case 'delete':
            $cc_id = $_GET['cc_id'];
            $sql = "delete from customercare where ccare_id='$cc_id'";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('删除成功!');location.href='ccare.php'</script>";
            }
            break;
        case 'update':
            $cc_id = $_GET['cc_id'];
            $sql = "select ct_id,ct_type from caretype";
            $sql1 = "select c_id,c_name from customer";
            $ctypeArr = getAllData($sql);
            $cArr = getAllData($sql1);
            $sql = "select * from customercare where ccare_id='$cc_id'";
            $res = getAllData($sql);
            $res = $res[0];
            include 'template/customer_care_alter.html';
            break;
        case 'update_check':
        // var_dump($_POST);
            $cc_id = $_GET['cc_id'];
            $care_theme = $_POST['care_theme'];
            $care_obj = $_POST['care_obj']+0;
            $care_nexttime = $_POST['care_nexttime'];
            $care_type = $_POST['care_type']+0;
            $care_comment = $_POST['care_comment'];
            $care_time = $_POST['care_time'];
            $sql = "update customercare set c_id='$care_obj',ccare_theme='$care_theme',ct_id='$care_type',ccare_time='$care_time',ccare_nexttime='$care_nexttime',ccare_comment='$care_comment' where ccare_id='$cc_id'";
            $res = dclData($sql);
            if($res){
                echo "<script>alert('更新成功!');location.href='ccare.php'</script>";
            }
            break;
    }
    include 'template/foot.html';